import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jet_rails/jr_app/jr_models/jr_article_model.dart';
import 'package:jet_rails/jr_app/jr_views/jr_discovery/jr_article_detail_view.dart';
import 'package:jet_rails/jr_app/jr_views/jr_discovery/jr_discovery_controller.dart';

class JrArticleWidget extends StatefulWidget {
  final JrArticleModel articleModel;
  final Widget? action;
  const JrArticleWidget({Key? key, required this.articleModel, this.action}) : super(key: key);

  @override
  State<JrArticleWidget> createState() => _JrArticleWidgetState();
}

class _JrArticleWidgetState extends State<JrArticleWidget> {
  final controller = Get.find<JrDiscoveryController>();
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () {
          Get.to(() => JrArticleDetailView(articleModel: widget.articleModel));
        },
        child: Container(
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              image:
                  const DecorationImage(image: AssetImage('assets/webp/img_activity_widget_bg.webp'), fit: BoxFit.fitWidth)),
          margin: EdgeInsets.only(top: 16.h),
          padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 16.h),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(widget.articleModel.title,
                  maxLines: 2, overflow: TextOverflow.ellipsis, style: TextStyle(color: Colors.white, fontSize: 16.sp)),
              SizedBox(height: 16.h),
              if (widget.articleModel.thumbnail != null)
                SizedBox(
                  height: 140.h,
                  child: ClipRRect(
                    borderRadius: BorderRadius.circular(10),
                    child: Image.asset(widget.articleModel.thumbnail!,
                        fit: BoxFit.cover, width: double.infinity, height: double.infinity),
                  ),
                ),
              SizedBox(height: 16.h),
              Row(
                children: [
                  CircleAvatar(
                    radius: 15,
                    backgroundImage: widget.articleModel.author?.avatar != null
                        ? NetworkImage(widget.articleModel.author!.avatar!)
                        : null,
                  ),
                  SizedBox(
                    width: 16.w,
                  ),
                  Expanded(
                    child: Text(
                      widget.articleModel.author?.nickname ?? '',
                      overflow: TextOverflow.ellipsis,
                      style: const TextStyle(color: Colors.white),
                    ),
                  ),
                  SizedBox(
                    width: 10.w,
                  ),
                  widget.action ?? Container(),
                ],
              )
            ],
          ),
        ));
  }
}
